import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class select {
    public static void main(String[] args) throws SQLException {

        //1.创建数据源对象
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/mdx?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("021642");
        //2.和数据库服务器建立连接
        Connection connection =dataSource.getConnection();
        System.out.println(connection);
        //3.创建SQL
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        //4.执行sql
        ResultSet resultSet = statement.executeQuery();
        //5.遍历结果集合
        while(resultSet.next()){
            //循环里针对
            System.out.println(resultSet.getInt("id"));
            System.out.println(resultSet.getString("name"));
        }
        //5.释放资源
        statement.close();
        connection.close();
    }
}
